package com.cap.dreamcircle.View.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cap.dreamcircle.Model.Bean.SystemMessageEntity;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.ViewHolderUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liucxu on 2018/3/22.
 */

public class SystemMessageAdapter extends BaseAdapter {

    private Context context;
    private List<SystemMessageEntity> datas = new ArrayList<>();

    public SystemMessageAdapter(Context c, List<SystemMessageEntity> sources) {
        context = c;
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_system_messages, null);
        }
        initViews(convertView, position);
        return convertView;
    }

    public void setDatas(List<SystemMessageEntity> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }

    private void initViews(View view, int position) {
        SystemMessageEntity entity = datas.get(position);
        TextView tv_system_messages = ViewHolderUtils.get(view, R.id.tv_system_messages);
        TextView tv_time = ViewHolderUtils.get(view, R.id.tv_time);
        tv_system_messages.setText(entity.getContent());
        tv_time.setText(entity.getCreateTime());
    }
}
